<?php
class ManagementPoetic extends CI_Controller {
    public function __construct(){
        parent::__construct();
    }

    public function index() {
        $this->load->view("back_end/home");
    }

    public function management() {
        require_once './application/core/sql.php';
        $id = $this->uri->segment(4);
        $ary_data = array();
        $ary_data['id'] = $id;

        if ($id == 'quan-ly-bai-tho') {
            $ary_param = array(
                'file_name' => 'back_end/poetic_search.sql',
                'key'       => 'get_category',
                'param'     => array()
            );

            $res = Sql::exec($ary_param);
            $ary_data['list_category'] = $res;

            if (empty($_POST['btnSearch']) == false) {
                $category_id = $_POST['category'];
                $poetic_caption = $_POST['poetic_caption'];
                $ary_rep = array();
                $param = array();
                if ($category_id != 0 || empty($poetic_caption) == false) {
                    $where = 'WHERE ';
                    if ($category_id != 0) {
                        $where .= 'poetic.id = ?';
                        $param[] = $category_id;
                    }

                    if (empty($poetic_caption) == false) {
                        if ($category_id != 0) {
                            $where .= 'AND ';
                        }
                        $where .= 'poetic.caption = ?';
                        $param[] = $poetic_caption;
                    }
                    $ary_rep['__CONDITION__'] = $where;
                } else {
                    $ary_rep['__CONDITION__'] = '';
                }

                $ary_param = array(
                    'file_name' => 'back_end/poetic_search.sql',
                    'key'       => 'get_poetic',
                    'param'     => $param
                );
                $res = Sql::exec($ary_param, $ary_rep);
                // var_dump($res);die;
                $ary_data['list_poetic'] = $res;
                $ary_data['action'] = 'search';
            }
        }
        $this->load->view("back_end/home", $ary_data);
    }

    public function edit() {
        require_once './application/core/sql.php';
        $id = $this->uri->segment(4);
        $ary_data = array();
        $ary_data['id'] = $id;
        if ($id == 'quan-ly-bai-tho') {
            $ary_data['action'] = 'edit';
            $ary_param = array(
                'file_name' => 'back_end/poetic_search.sql',
                'key'       => 'get_category',
                'param'     => array()
            );
            $res = Sql::exec($ary_param);
            $ary_data['list_category'] = $res;

            $poetic_id = $this->uri->segment(5);
            $ary_param = array(
                'file_name' => 'back_end/poetic_edit.sql',
                'key'       => 'get_poetic',
                'param'     => array($poetic_id)
            );
            $res = Sql::exec($ary_param);
            $ary_data['poetic'] = $res[0];

            if (empty($_POST['btnUpdate']) == false) {
                // header('Content-Type: text/html; charset=utf-8');
                // echo "<pre>";var_dump(nl2br($_POST['poetic_content']));die;
                require_once './application/core/Common.php';
                $ary_param = array(
                'file_name' => 'back_end/poetic_edit.sql',
                'key'       => 'update_poetic',
                'param'     => array(
                                    $_POST['category'],
                                    Common::vnToLatin($_POST['poetic_caption'], true),
                                    $_POST['poetic_caption'],
                                    '123456',
                                    $poetic_id
                               )
                );
                $res = Sql::update($ary_param);

                $ary_param = array(
                'file_name' => 'back_end/poetic_edit.sql',
                'key'       => 'update_poetic_detail',
                'param'     => array(
                                    $_POST['poetic_content'],
                                    $poetic_id
                               )
                );
                $res = Sql::update($ary_param);

                $this->load->helper('url');
                redirect('/back_end/managementpoetic/management/quan-ly-bai-tho', 'refresh');
            }
        }
        $this->load->view("back_end/home", $ary_data);
    }

    public function insert() {
        require_once './application/core/sql.php';
        $id = $this->uri->segment(4);
        $ary_data = array();
        $ary_data['id'] = $id;
        $this->load->view("back_end/home", $ary_data);
    }

}
?>